package ReSofting.it.GoldDirector.Helpers;


	import java.lang.reflect.Type;
	import java.net.SocketException;
import java.text.SimpleDateFormat;
	import java.util.ArrayList;
import java.util.Calendar;

	import org.apache.http.HttpEntity;
	import org.apache.http.HttpResponse;
	import org.apache.http.client.HttpClient;
	import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
	import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
	import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;


import ReSofting.it.GoldDirector.Interfaces.IRemoteSaveCall;
import ReSofting.it.GoldDirector.JsonTrans.JsonTransformer_myOrdine;
import ReSofting.it.GoldDirector.Model.myOrdine;

	import android.os.AsyncTask;
import android.util.Log;



	public class SaveOrderAsync extends AsyncTask<String, Void, HttpEntity> {
		 
	    private final static String TAG = GetDataAsync.class.getName();
	    
	    IRemoteSaveCall<HttpEntity> listener;
	    
	    myOrdine _Order = null;
	 
	    
	    public SaveOrderAsync(IRemoteSaveCall<HttpEntity> listener, myOrdine pOrder) {
	        this.listener = listener;
	        _Order =  pOrder;
	    }
	 
	    @Override
	    protected HttpEntity doInBackground(String... urls) 
	    {
	    	HttpEntity entity = null;
	    	HttpEntity result = null;
	        String url = urls[0];
	        	        
	        
	       // HttpHost host = new HttpHost("10.0.2.2" , 8096);
	        //HttpGet get = new HttpGet( "/Services/OrdersSvc.svc/GetMenu" ); //url
	        
	        HttpParams httpParameters = new BasicHttpParams();
		     HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
		     HttpConnectionParams.setSoTimeout(httpParameters, 20000);
	               
	        DefaultHttpClient httpClient = new DefaultHttpClient();
	        httpClient.setParams(httpParameters);
	        
	        //"http://10.0.2.2:8096/Services/OrdersSvc.svc/SaveOrder"
	        HttpPost request = new HttpPost(url );//
	        request.setHeader("Accept", "application/json");
	        request.setHeader("Content-type", "application/json");
	               
	        boolean isValid = true;
	        
	        // Data validation goes here
	 
	        if (! isValid) return result;
	 
	        	try{
        		
	        		Log.d("Transforming order in Json", "Start");
	            //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
	        	 //String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
	      	   
	        	 // trasformo in json
	        	 JsonTransformer_myOrdine jsonner = new JsonTransformer_myOrdine();
	        	 StringEntity dati = jsonner.fromMyOrdine_ToJson(_Order);
	          	            
	        	 if (dati == null )
	        		 throw new Exception("Error in transforming order in Json");
	            
	        	 Log.d("Transformed order in Json", dati.toString());
	 

	            request.setEntity(dati);
	 
	            // Send request to WCF service	           
	            HttpResponse response = httpClient.execute(request);
	 
	            result = response.getEntity();
	            	            
	            Log.d("Ordine salvato ? ", "Status : " + response.getStatusLine().getStatusCode());
	            	 
	        
	        	}
	        catch (Exception e) {
	        e.printStackTrace();
	        }
	       
	        return result ;
	    }
	 
	    protected void onPostExecute(HttpEntity result) 
	    {
	       listener.onRemoteSaveCallCompleted(result);
	    }
	 
	}

